<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirm-btn="disabled?null:'确认'"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form :disabled="disabled" label-align="top" ref="form" :data="formData" :colon="true" labelWidth="100px" :rules="rules">
        <t-form-item label="模版类型" name="type">
          <t-select clearable filterable v-model="formData.type" :options="dict.type.review_template_type"></t-select>
        </t-form-item>
        <t-form-item label="内容" name="content">
          <tiny-mce-editor v-model="formData.content"></tiny-mce-editor>
        </t-form-item>
        <t-form-item label="是否启用" name="status">
          <t-switch
            size="large"
            v-model="formData.status"
            :label="['是', '否']"
            :customValue="['1', '0']"
          ></t-switch>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import TinyMceEditor from "@/components/TinyMceEditor/index.vue";

export default {
  name: "add-edit-tzgg",
  dicts: ['review_template_type'],
  components:{
    TinyMceEditor
  },
  props: ['header', 'objId', 'parentId', 'disabled'],
  data(){
    return {
      visible: true,
      serviceOptions: [],
      fileLists: [],
      formData:{
        type: '',
        content: '',
        status: '1',
      },
      rules:{
        type: [{required: true, message: "模版类型不能为空"}],
        content: [{required: true, message: "内容不能为空"}],
        status: [{required: true, message: "是否启用不能为空"}],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj() {
      this.$api.kjxm.template.reviewGet(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          if (this.objId) {
            this.$api.kjxm.template.reviewUpdate(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.$message.error(e.toString());
            });
          } else {
            this.$api.kjxm.template.reviewAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
